﻿using ApiLib.Helpers;
using Furion.Templates;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Caching.Memory;
using SqlSugar;

namespace ApiLib.AppStart;

/// <summary>
/// 启动类，详情：<see href="http://172.20.30.76:16003/zh/">ApiLib Docs</see>
/// <!--
/// <para>启动目录<c>App.HostEnvironment.ContentRootPath</c>对应<c>D:\Code\C2_Api\WebDemo\</c></para>
/// <para>运行目录<c>AppContext.BaseDirectory</c>对应<c>D:\Code\C2_Api\WebDemo\bin\Debug\net6.0\</c></para>
/// -->
/// </summary>
public static class WebApp
{
    /// <summary>
    /// 数据库
    /// </summary>
    public static ISqlSugarClient Db => DbHelper.Db;

    /// <summary>
    /// 数据库
    /// </summary>
    public static SqlSugarScope? DbScope => DbHelper.DbScope;

    /// <summary>
    /// 缓存
    /// </summary>
    public static IMemoryCache Cache => DbHelper.Cache;

    /// <summary>
    /// 启动
    /// </summary>
    /// <param name="config">配置对象</param>
    public static void Start(AppCoreConfig? config = null)
    {
        GlobalConfig.AppCoreConfig = config ?? new AppCoreConfig();
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine(GlobalConfig.Banner);
        Console.ForegroundColor = ConsoleColor.Blue;

        var builder = WebApplication.CreateBuilder().Inject();

        {
            //显示基础信息
            var appInfo = GlobalConfig.AppInfo;
            if (appInfo != null)
            {
                var template = TP.Wrapper(appInfo.Title, appInfo.Description,
                    $"##作者## {appInfo.Author}",
                    $"##当前版本## {appInfo.Version}",
                    $"##文档地址## {appInfo.Document}",
                    $"##Copyright## {appInfo.CompanyName}");
                Console.WriteLine(template);
                Console.ResetColor();
            }
        }

        {
            // var httpPort = App.GetConfig<int>("HttpPort");
            var httpPort = GlobalConfig.AppConfig?.HttpPort ?? 0;
            // var httpsPort = App.GetConfig<int>("HttpsPort");
            var httpsPort = GlobalConfig.AppConfig?.HttpsPort ?? 0;
            /*if (GlobalConfig.AppCoreConfig != null)
            {
                if (httpPort == 0 && GlobalConfig.AppCoreConfig.HttpPort > 0)
                {
                    httpPort = GlobalConfig.AppCoreConfig.HttpPort;
                }

                if (httpsPort == 0 && GlobalConfig.AppCoreConfig.HttpsPort > 0)
                {
                    httpsPort = GlobalConfig.AppCoreConfig.HttpsPort;
                }
            }*/

            if (httpPort > 0 && httpsPort > 0)
            {
                builder.WebHost.UseUrls($"http://*:{httpPort}", $"https://*:{httpsPort}");
            }
            else if (httpPort > 0)
            {
                builder.WebHost.UseUrls($"http://*:{httpPort}");
            }
            else if (httpsPort > 0)
            {
                builder.WebHost.UseUrls($"https://*:{httpsPort}");
            }
        }

        var app = builder.Build();

        // var host = App.Configuration["Host"] ?? BaseConfig.MyAppConfig?.Host;
        var host = GlobalConfig.AppConfig?.Host;
        app.Run(host);
    }
}